package com.dave.admin.component.log;


import cn.hutool.core.util.IdUtil;
import org.slf4j.MDC;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * traceId 跟踪定位，可以根据traceId跟踪整条调用链
 * DMC是配置logback和log4j使用的，使用方式和ThreadContext差不多
 * @author Dave
 */
@Configuration
@Order(Integer.MIN_VALUE)
public class RequestLogFilter extends OncePerRequestFilter {

    /**
     * 链路标识
     */
    private final static String TRACE_ID = "traceId";

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        MDC.put(TRACE_ID,IdUtil.simpleUUID());
        filterChain.doFilter(request, response);
    }
}
